﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimViec.backend
{
    public partial class QuocGia : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        public void LoadData()
        {
            rptQuocGia.DataSource = BLL.Admin.QuocGiaHelper.List_QuocGia();
            rptQuocGia.DataBind();
        }
        protected void rptQuocGia_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                int quocgiaid = int.Parse(e.CommandArgument.ToString());
                lbQuocGiaId.Text = quocgiaid.ToString();
                DAL.tbl_DM_QuocGia quocgia = BLL.Admin.QuocGiaHelper.Get_QuocGiaId(quocgiaid);
                txtTenQuocGia.Text = quocgia.Ten;
                btSave.Visible = false;
                btUpdate.Visible = true;
            }
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTenQuocGia.Text))
            { 
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa nhập tên quốc gia!');", true);
                return;
            }
            BLL.Admin.QuocGiaHelper.Add_QuocGia(txtTenQuocGia.Text);
            LoadData();
            LoadReset();
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTenQuocGia.Text))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Chưa nhập tên quốc gia!');", true);
                return;
            }
            BLL.Admin.QuocGiaHelper.Update_QuocGia(int.Parse(lbQuocGiaId.Text),txtTenQuocGia.Text);
            LoadData();
            LoadReset();
        }
        public void LoadReset()
        {
            txtTenQuocGia.Text = "";
            btSave.Visible = true;
            btUpdate.Visible = false;
        }
        protected void btCancel_Click(object sender, EventArgs e)
        {
            LoadReset();
        }
    }
}